package net.oschina.victor.repsotory;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import net.oschina.victor.entity.Organization;

@Repository
public interface OrganizationRepsotory
		extends PagingAndSortingRepository<Organization, Long>, JpaSpecificationExecutor<Organization> {

	@Query(value = "select o from Organization o where o.parentIds not like '%,' + :id + ',%'")
	public List<Organization> findAllWithExclude(String id);

	@Modifying
	@Query(value = "update Organization o set o.parentIds=concat(:targetIds, substring(o.parentIds, length(:sourceIds)+1)) where o.parentIds like concat(:sourceIds , '%') ")
	public void moveSourceDescendants(String targetIds, String sourceIds);

}
